package com.mkyong;

import java.io.File;
import java.util.List;

import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.springframework.batch.core.ItemWriteListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.StringUtils;

public class CustomedItemWriterListener implements ItemWriteListener<User> {

	private JavaMailSender senderImpl;
	
	private String from;
	private String to;
	private String cc;

	public void setFrom(String from) {
		this.from = from;
	}

	public void setTo(String to) {
		this.to = to;
	}
	public void setCc(String cc) {
		this.cc = cc;
	}

	public void setSenderImpl(JavaMailSender senderImpl) {
		this.senderImpl = senderImpl;
	}

	@Override
	public void beforeWrite(List<? extends User> items) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void afterWrite(List<? extends User> items) {
		// TODO Auto-generated method stub
		String filePath = ExcelItemWriter.getFilePath();
		System.out.println("send attachement "+filePath+" to dengzhongliang@300.cn");
		try{
			MailHandler mailHander = new MailHandler();
			mailHander.setMailSender(senderImpl);
			mailHander.sendMail(from, StringUtils.isEmpty(to) ? null : to.split(","), StringUtils.isEmpty(cc) ? null : cc.split(","), null, null, filePath);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	@Override
	public void onWriteError(Exception exception, List<? extends User> items) {
		// TODO Auto-generated method stub
		
	}

}
